package testerbeans;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import dao.InterviewDAORemote;
import dto.InterviewDTO;
import dto.InterviewViewDto;

import emailbeans.EmployeeBean;

@ManagedBean(name = "emailBean")
@SessionScoped
public class EmailBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{employeeBean}")
	private EmployeeBean employeeBean;

	@ManagedProperty(value = "#{interviewDto}")
	InterviewViewDto interviewDto;

	@EJB
	InterviewDAORemote interviewDAO;

	private InterviewDTO idto;

	/**
	 * The link for candidate The duration of test.
	 */
	private String link;
	private int duration;

	private String comment;
	private String accepted;

	private int idCandidate;
	private int templateId;


	private Boolean delete=false;


	/**
	 * Email Information who to send, the subject and the message that are.
	 * completed in emailCandidateContent.xhtml file.
	 */
	private String email;
	private String subject = "Msg Systems Invitation";
	private String message = null;

	/**
	 * The proxy.
	 */
	private String smtpServ;
	private String from;
	int emailForCandidate = 0;

	private int selectedTemplateID;
	private int selectedRoomID;
	private int employeeId;
	private Boolean fromRedirect;

	/**
	 * The constructor where is set The SmtpServ. Setting the proxy.
	 */
	public EmailBean() {
		setSmtpServ("msgc4i.msg.de");
	}

	/**
	 * Getter for interviewDto
	 * 
	 * @return interviewDto
	 */

	public InterviewViewDto getInterviewDto() {
		return interviewDto;
	}

	/**
	 * Setter for InterviewDto
	 * @param interviewDto
	 */
	public void setInterviewDto(InterviewViewDto interviewDto) {
		this.interviewDto = interviewDto;
	}

	/**
	 * Setters and getters for link. This link will be send to candidate.
	 * 
	 * @return
	 */
	public String getLink() {
		return link;
	}

	/**
	 * Setter for link
	 * @param link
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * getter for EmployeeBean where are the information of logged user.
	 * 
	 * @return
	 */
	public EmployeeBean getEmployeeBean() {
		return employeeBean;
	}

	/**
	 * setter for EmployeeBean where are the information of logged user.
	 * 
	 * @param employeeBean
	 */

	public void setEmployeeBean(EmployeeBean employeeBean) {
		this.from = employeeBean.getEmail();
		this.employeeBean = employeeBean;
	}

	/**
	 * getter for accepted atribute.
	 * 
	 * @return
	 */
	public String getAccepted() {
		return accepted;
	}

	/**
	 * setter for accepted atribute.
	 * 
	 * @return
	 */
	public void setAccepted(String accepted) {
		this.accepted = accepted;
	}

	/**
	 * getter for comment atribute.
	 * 
	 * @return
	 */
	
	public String getComment() {
		return comment;
	}

	/**
	 * setter for accepted command.
	 * 
	 * @return
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * getter for IdCandidate.
	 * 
	 * @return id for candidate.
	 */
	public int getIdCandidate() {
		return idCandidate;
	}

	/**
	 * 
	 * @param idCandidate id for candidate.
	 */
	public void setIdCandidate(int idCandidate) {
		this.idCandidate = idCandidate;
	}

	/**
	 * getter for templateId.
	 * 
	 * @return
	 */
	public int getTemplateId() {
		return templateId;
	}

	/**
	 * setter for templateId command.
	 * 
	 * @return the id for the template of the test.
	 */
	public void setTemplateId(int templateId) {
		this.templateId = templateId;
	}

	/**
	 * Getters and setter for email. This represent the address of candidate.
	 * 
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	
	/**
	 * Getter for email 
	 * @return email for candidate from InterviewDTO.
	 */
	public String getEmail() {
		return interviewDto.getCandidate().getEmail();
	}

	/**
	 * getter for duration of a test.
	 * 
	 * @return
	 */
	public int getDuration() {

		return duration;
	}

	/**
	 * setter for duration of a test.
	 * 
	 * @return
	 */
	public void setDuration(int duration) {
		this.duration = duration;
	}

	/**
	 * getter for email message. There we compose the message.
	 * 
	 * @return
	 */
	public String getMessage() {
		return " Hello "
				+ interviewDto.getCandidate().getFirstName()
				+ " "
				+ interviewDto.getCandidate().getLastName()
				+ " ! As a result of your application for a new job in our company, we would like to invite you to attend an interview on "
				+ interviewDto.getDate()
				+ " at "
				+ interviewDto.getStartTime()
				+ " . Our center located at Str. Samuel Brassai, nr.9, Cluj-Napoca"
				+ ". \n\n "
				+ "You will have an test-interview "
				+ interviewDto.getTemplate().getTitle()
				+ " in the room "
				+ interviewDto.getRoom().getAddress()
				+ " . The test will last about "
				+ duration
				+ " minutes. Could you please bring a copy of your identity documents as well as a copy of your certificates with you to the interview. \n\n Should the date or time be inconvenient, please contact on email: msg.info-office-cluj@msg-systems.com in order to arrange another appointment. We look forward to seeing you. \n\n"
				+ "To start the test you will have to access next link: \n"
				+ link + "\n\n" + "MSG SYSTEMS ROMANIA \n\n ";
	}

	/**
	 * Method that create the message and send it to specified address.
	 * 
	 * @return
	 */
	public void sendEmail() {
		try {
			Properties props = System.getProperties();

			// -- Attaching to default Session, or we could start a new one --
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", smtpServ);
			props.put("mail.smtp.auth", "true");

			Authenticator auth = new SMTPAuthenticator();

			Session session = Session.getInstance(props, auth);

			// -- Create a new message --
			Message msg = new MimeMessage(session);
			// -- Set the FROM and TO fields --
			msg.setFrom(new InternetAddress(from, "MSG Systems"));

			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
					email, false));

			msg.setSubject(subject);

			// -- Set some other header information --
			msg.setHeader("MyMail", "Mr. XYZ");
			msg.setSentDate(new Date());

			// create the message part
			MimeBodyPart messageBodyPart = new MimeBodyPart();
			// String fileAttachment = "Z:/Projects/Orders/PrimewJSP.7z";
			// fill message

			messageBodyPart.setText(message);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			// Put parts in message
			msg.setContent(multipart);

			// -- Send the message --
			Transport.send(msg);

			// write interview into the database
			System.out.println("Invoking addInterview method");
			

			System.out.println("from" + this.from);
			System.out.println("Message sent to" + email + " OK.");
			interviewDto.setFromRedirect(false);	

			delete=true;
		//	getInterviewDto().setFromRedirect(true);


	
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Exception " + ex);
		}

	}


	/**
	 * Inner class that manage the autentication on email account
	 * 
	 * 
	 */
	// Also include an inner class that is used for authentication purposes
	private class SMTPAuthenticator extends javax.mail.Authenticator {

		@Override
		public PasswordAuthentication getPasswordAuthentication() {

			

			return new PasswordAuthentication("vornicub", "vornicub");
		}
	}

	/**
	 * Getters and setters for from attribute.
	 * 
	 * @param from
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * getFrom
	 * @return
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * Getters and setters for Subject attribute. It represent the subject of
	 * message to send.
	 * 
	 * @param subject
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * get subject for message.
	 * @return
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * Getters and setters for message attribute. It represent the message to send.
	 * 
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * Setters and Getters for proxy.
	 * 
	 * @param smtpServ
	 */
	public void setSmtpServ(String smtpServ) {
		this.smtpServ = smtpServ;
	}

	/**
	 * Getter for proxy.
	 * @return
	 */
	public String getSmtpServ() {
		return smtpServ;
	}

	/**
	 * Setters and Getters for InterviewDTO.
	 * 
	 * @param idto
	 */
	public void setIdto(InterviewDTO idto) {
		this.idto = idto;
	}

	
	/**
	 * Getter InterviewDTO
	 * @return
	 */
	public InterviewDTO getIdto() {
		return idto;
	}

	/**
	 * Setters and Getters
	 * 
	 * @param selectedTemplateID
	 */
	public void setSelectedTemplateID(int selectedTemplateID) {
		this.selectedTemplateID = selectedTemplateID;
	}

	/**
	 * Getter for selected Template ID
	 * @return
	 */
	public int getSelectedTemplateID() {
		return selectedTemplateID;
	}

	/**
	 * Setters and Getters
	 * 
	 * @param selectedRoomID
	 */
	public void setSelectedRoomID(int selectedRoomID) {
		this.selectedRoomID = selectedRoomID;
	}

	/**
	 * Getter for id of selected Room
	 * @return
	 */
	public int getSelectedRoomID() {
		return selectedRoomID;
	}

	/**
	 * Setters and Getters
	 * 
	 * @param employeeId
	 */
	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	/**
	 * Getter for Id of employee
	 * @return employee id
	 */
	public int getEmployeeId() {
		return employeeId;
	}

/**
 * setFromRedirect
 * @param fromRedirect
 */
	public void setFromRedirect(Boolean fromRedirect) {
		this.fromRedirect = fromRedirect;
	}
/**
 * getFromRedirect
 * @return
 */
	public Boolean getFromRedirect() {
		return fromRedirect;
	}

public void setDelete(Boolean delete) {
	System.out.println("Delete in set is "+delete);
	this.delete = delete;
}

public Boolean getDelete() {
	System.out.println("Delete in get is "+delete);
	return delete;
}


}
